package com.service;

import com.mapper.TotalShoppingPriceViewMapper;
import com.pojo.view.TotalShoppingPriceView;
import com.service.iter.IBillService;
import com.util.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 账单服务实现
 *
 * @author 蔡文欣
 * @date 2023/07/03 15:46:38
 */
@Service
@Transactional
public class BillServiceImpl implements IBillService {
    @Autowired
    private TotalShoppingPriceViewMapper totalShoppingPriceViewMapper;

    /**
     * 通过电话获取价格
     *
     * @param phone 电话
     * @return {@link Res}
     */
    @Override
    public Res byPhoneGetPrice(String phone) {
        Res res = new Res();
        List<TotalShoppingPriceView> totalShoppingPriceViews = totalShoppingPriceViewMapper.byPhoneGetPrice(phone);
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (TotalShoppingPriceView view : totalShoppingPriceViews) {
            System.out.println(view);
            totalPrice = totalPrice.add(view.getTotalPrice());
        }
        res.setCode(200);
        System.out.println(totalPrice);
        if (totalPrice.equals(new BigDecimal(0))) {
            res.setData("尚未选购商品");
        } else {
            res.setData(totalPrice);
        }
        return res;
    }
}
